package com.taotao.portal.controller;
/**
 * @author lyx
 * @version 2019年1月7日 上午10:34:00
 */

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.CartService;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    
    @RequestMapping("/add/{itemId}")
    public String addCartItem(@PathVariable Long itemId,@RequestParam(defaultValue="1")Integer num,HttpServletRequest request,HttpServletResponse response) {
        TaotaoResult result=cartService.addCartItem(itemId, num, request, response);
        if(result.getStatus()==200) {
            return "redirect:/cart/success.html";
        }
        return "ERROR";
    }
    @RequestMapping("/success")
    public String showSuccess() {
        return "cartSuccess";
    }
    
    @RequestMapping("cart")
    public String showCart(HttpServletRequest request,HttpServletResponse response,Model model) {
        List<CartItem> list= cartService.getCartItemList(request, response);
        model.addAttribute("cartList", list);
        return "cart";
    }
    
    @RequestMapping("/delete/{itemId}")
    public String deleteCartItem(@PathVariable Long itemId,HttpServletRequest request,HttpServletResponse response) {
        cartService.deleteCartItem(itemId, request, response);
        return "redirect:/cart/cart.html";
    }
    
}
